package opencm;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import opencm.cli.TelaPrincipal;

public final class Main {

	public static final RepositorioConsulta repositorioConsulta = new RepositorioConsulta();
	public static final RepositorioMedico repositorioMedico = new RepositorioMedico();
	public static final RepositorioPaciente repositorioPaciente = new RepositorioPaciente();
	public static final RepositorioPlanoSaude repositorioPlanoSaude = new RepositorioPlanoSaude();
	
	private static final String ARQUIVO_CONSULTA = "consulta.ser";
	private static final String ARQUIVO_MEDICO = "medico.ser";
	private static final String ARQUIVO_PACIENTE = "paciente.ser";
	private static final String ARQUIVO_PLANO_SAUDE = "plano_saude.ser";

	public void iniciarCLI() {
		recuperarDados();
		try {
			new TelaPrincipal().executar();
		} finally {
			persistirDados();
		}
	}
		
	private void recuperarDados() {
		recuperar(repositorioConsulta, ARQUIVO_CONSULTA);
		recuperar(repositorioMedico, ARQUIVO_MEDICO);
		recuperar(repositorioPaciente, ARQUIVO_PACIENTE);
		recuperar(repositorioPlanoSaude, ARQUIVO_PLANO_SAUDE);
		
	}
	
	private void persistirDados() {
		persistir(repositorioConsulta, ARQUIVO_CONSULTA);
		persistir(repositorioMedico, ARQUIVO_MEDICO);
		persistir(repositorioPaciente, ARQUIVO_PACIENTE);
		persistir(repositorioPlanoSaude, ARQUIVO_PLANO_SAUDE);
	}
	
	private <E> void recuperar(Repositorio<E> repository, String filename) {
        try {
        	@SuppressWarnings("unchecked")
            ArrayList<E> items = (ArrayList<E>) readObject(filename);
        	repository.adicionarItens(items);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	private <E> void persistir(Repositorio<E> repository, String filename) {
        try {
        	writeObject(repository.getItens(), filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	private void writeObject(Object object, String filename) {
		ObjectOutputStream out = null;
		
		try {
			out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if (out != null) {
			try {
				out.writeObject(object);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private Object readObject(String filename) throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream in = null;
		try {
			in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filename)));
			return in.readObject();
		} catch (FileNotFoundException e) {
			File f = new File(filename);
			f.createNewFile();
			return null;
		} finally {
			in.close();
		}
	}
	
	public static void main(String args[]) {
		Main app = new Main();
		app.iniciarCLI();
	}
	
}
